package com.ibm.framework.exception.aop;

import com.ibm.framework.exception.ExceptionProcessDispatch;
import com.ibm.framework.exception.logger.ExceptionLogger;
import com.ibm.framework.exception.logger.LogMessageBuilder;
import com.ibm.framework.exception.model.RawExceptionInfo;
import org.aspectj.lang.JoinPoint;

/**
 * 功能描述：统一异常处理报告
 * 
 * @author v_pinzhao
 * 
 */
public class ExceptionProcessAdvice {
    /**
     * 异常处理拦截器
     */
    private ExceptionProcessDispatch exceptionProcessDispatch;

    /**
     * 
     * 功能描述：异常抛出后输出日志
     * 
     * @param joinPoint 切入点
     * @param ex 异常
     * @throws Exception 异常描述
     */
    public void logAfterThrowing(JoinPoint joinPoint, Throwable ex) throws Exception {
        try {
            String interceptedClass = joinPoint.getTarget().getClass().getName();
            String interceptedInterface = joinPoint.getSignature().toString();
            Object[] parameterValue = joinPoint.getArgs();
            RawExceptionInfo rawEx = new RawExceptionInfo(interceptedClass, interceptedInterface, parameterValue, ex);

            exceptionProcessDispatch.process(rawEx);
        } catch (Exception e) {
            String msg = LogMessageBuilder.build("Fail to handle the intercepted exception!", e);
            ExceptionLogger.log(msg);
        }
    }

    /**
     * 获取异常处理拦截器
     * 
     * @return 异常处理拦截器
     */
    public ExceptionProcessDispatch getExceptionProcessDispatch() {
        return exceptionProcessDispatch;
    }

    /**
     * 设置异常处理拦截器
     * 
     * @param exceptionProcessDispatch 异常处理拦截器
     */
    public void setExceptionProcessDispatch(ExceptionProcessDispatch exceptionProcessDispatch) {
        this.exceptionProcessDispatch = exceptionProcessDispatch;
    }
}